
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Data;
using System.Data.SqlClient;

using Swaf;

namespace Swaf.DataAccess
{
	/// <summary>
	/// Summary description for SqlServerHelper.
	/// </summary>
	public class SqlServerHelper : IAdoNetHelper
	{
		public SqlServerHelper()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		#region IAdoNetHelper Members

		public void addCommandParam(IApplication app, System.Data.IDbCommand cmd, string name, string type, int size, ParameterDirection dir)
		{
			if(cmd is SqlCommand)
			{
				SqlCommand sqlcmd = (SqlCommand)cmd;
				SqlDbType paramType = (SqlDbType)app.factories["SqlServerDbTypes"].createFromNickName(type);
				IDbDataParameter p = sqlcmd.Parameters.Add("@"+name, paramType, size);
				if(dir != ParameterDirection.Input)
					p.Direction = dir;

			}

		}

		public void addCommandParam(IApplication app, IDbCommand cmd, string name, string type, int size, ParameterDirection dir, object val)
		{
			if(cmd is SqlCommand)
			{
				SqlCommand sqlcmd = (SqlCommand)cmd;
				SqlDbType paramType = (SqlDbType)app.factories["SqlServerDbTypes"].createFromNickName(type);;
				IDbDataParameter p = sqlcmd.Parameters.Add("@"+name, paramType, size);
				p.Value = val==null?DBNull.Value:val;
				if(dir != ParameterDirection.Input)
					p.Direction = dir;
			}
		}
		public object getParamValue(IDataParameterCollection parameters, string paramName)
		{
			object paramValue=null;
			paramValue = ((System.Data.SqlClient.SqlParameter)((parameters[@"@"+paramName]))).Value;
			if (paramValue.GetType() == typeof(System.Guid))
				paramValue = new BGuid.Guid((System.Guid) paramValue);
			return paramValue;
		}

		public bool allowParamOutputMapping(IDataParameterCollection parameters, string paramName)
		{
			return true;
		}

		#endregion
	}
}
